package com.cityeye.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.cityeye.dao.QueryDao;
import com.cityeye.util.CityUtil;

/**
 * Mmessage entity. @author MyEclipse Persistence Tools
 */

public class Mmessage extends com.cityeye.util.IEntity implements
		java.io.Serializable {

	public static final String FILE_SPERATOR =",";
	// Fields

	private Integer id;
	private String no;	

	private Subissuetype subissuetype;
	private Patrol patrol;
	private Department department;
	private Integer quConfirmed = 0;
	private String sender = "";
	private Integer msgtype = 0;
	private String msgtitle = "";
	private String msgattachfiles = "";
	private Timestamp arrivetime;
	private Timestamp completetime;
	private Integer msgstate = 0;
	private String shiComments = "";
	private String quComments = "";
	private String jieComments = "";
	private String submitfiles = "";
	private String finalcomments = "";
	private Integer completestate = 0;

	private Department qudepartment;

	private Department problemJD;
	// Constructors

	public Department getProblemJD() {
		return problemJD;
	}

	public void setProblemJD(Department jiedao) {
		this.problemJD = jiedao;
	}

	public Department getQudepartment() {
		return qudepartment;
	}

	public void setQudepartment(Department quDepartment) {
		this.qudepartment = quDepartment;
	}

	/** default constructor */
	public Mmessage() {
	}

	/** minimal constructor */
	public Mmessage(Integer id, Integer quConfirmed, Integer msgtype,
			Integer msgstate, Integer completestate) {
		this.id = id;
		this.quConfirmed = quConfirmed;
		this.msgtype = msgtype;
		this.msgstate = msgstate;
		this.completestate = completestate;
	}

	/** full constructor */
	public Mmessage(Integer id, Subissuetype subissuetype, Patrol patrol,
			Department department, Integer quConfirmed, String sender,
			Integer msgtype, String msgtitle, String msgattachfiles,
			Timestamp arrivetime, Integer msgstate, String shiComments,
			String quComments, String jieComments, String submitfiles,
			String finalcomments, Integer completestate) {
		this.id = id;
		this.subissuetype = subissuetype;
		this.patrol = patrol;
		this.department = department;
		this.quConfirmed = quConfirmed;
		this.sender = sender;
		this.msgtype = msgtype;
		this.msgtitle = msgtitle;
		this.msgattachfiles = msgattachfiles;
		this.arrivetime = arrivetime;
		this.msgstate = msgstate;
		this.shiComments = shiComments;
		this.quComments = quComments;
		this.jieComments = jieComments;
		this.submitfiles = submitfiles;
		this.finalcomments = finalcomments;
		this.completestate = completestate;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String number) {
		this.no = number;
	}
	
	/*
	 * Override to provide default name display in page.
	 * 
	 * @see com.cityeye.util.IEntity#getName()
	 */
	public String getName() {
		return getMsgtitle();
	}

	public Subissuetype getSubissuetype() {
		return this.subissuetype;
	}

	public void setSubissuetype(Subissuetype subissuetype) {
		this.subissuetype = subissuetype;
	}

	public Patrol getPatrol() {
		return this.patrol;
	}

	public void setPatrol(Patrol patrol) {
		this.patrol = patrol;
	}

	public Department getDepartment() {
		return this.department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Integer getQuConfirmed() {
		return this.quConfirmed;
	}

	public void setQuConfirmed(Integer quConfirmed) {
		this.quConfirmed = quConfirmed;
	}

	public String getSender() {
		return this.sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public Integer getMsgtype() {
		return this.msgtype;
	}

	public void setMsgtype(Integer msgtype) {
		this.msgtype = msgtype;
	}

	public String getMsgtitle() {
		return this.msgtitle;
	}

	public void setMsgtitle(String msgtitle) {
		this.msgtitle = msgtitle;
	}

	public String getMsgattachfiles() {
		return this.msgattachfiles;
	}

	public void setMsgattachfiles(String msgattachfiles) {
		this.msgattachfiles = msgattachfiles;
	}

	public Timestamp getArrivetime() {
		return this.arrivetime;
	}
	
	public String getArrivetimeStr() {
		if(arrivetime!=null)
			return QueryDao.dateFormater.format(arrivetime);
		else
			return "";
	}

	public void setArrivetime(Timestamp arrivetime) {
		this.arrivetime = arrivetime;
	}

	public Timestamp getCompletetime() {
		return completetime;
	}
	
	public String getCompletetimeStr() {
		if(completetime!=null)
			return QueryDao.dateFormater.format(completetime);
		else
			return "";
	}	

	public void setCompletetime(Timestamp completetime) {
		this.completetime = completetime;
	}

	public Integer getMsgstate() {
		return this.msgstate;
	}

	public void setMsgstate(Integer msgstate) {
		this.msgstate = msgstate;
	}

	public String getShiComments() {
		return this.shiComments;
	}

	public void setShiComments(String shiComments) {
		this.shiComments = shiComments;
	}

	public String getQuComments() {
		return this.quComments;
	}

	public void setQuComments(String quComments) {
		this.quComments = quComments;
	}

	public String getJieComments() {
		return this.jieComments;
	}

	public void setJieComments(String jieComments) {
		this.jieComments = jieComments;
	}

	public String getSubmitfiles() {
		return this.submitfiles;
	}

	public void setSubmitfiles(String submitfiles) {
		this.submitfiles = submitfiles;
	}

	public String getFinalcomments() {
		return this.finalcomments;
	}

	public void setFinalcomments(String finalcomments) {
		this.finalcomments = finalcomments;
	}

	public Integer getCompletestate() {
		return this.completestate;
	}

	public void setCompletestate(Integer completestate) {
		this.completestate = completestate;
	}

	public String[] getImages() {
		if (this.getMsgattachfiles() != null) {
			String[] files = getMsgattachfiles().split(FILE_SPERATOR);
			String[] results = new String[files.length];
			for (int i = 0; i < files.length; i++) {
				results[i] = CityUtil.getMMSRelativePath(files[i]);
			}
			return results;
		}
		return new String[0];
	}

	public String[] getUploadImages() {
		if (this.getSubmitfiles() != null) {
			String[] files = this.getSubmitfiles().split(FILE_SPERATOR);
			String[] results = new String[files.length];
			for (int i = 0; i < files.length; i++) {
				results[i] = CityUtil.getUpfilesRelativePath(files[i]);
			}
			return results;
		}
		return new String[0];
	}

	public String removeImage(String[] existsImgs, String filepath, boolean isMMS) {
		StringBuffer sb = new StringBuffer("");
		if (existsImgs != null && existsImgs.length > 0) {
			String relFilePath = CityUtil.getMMSRelativePath(filepath);
			if(!isMMS){
				relFilePath = CityUtil.getUpfilesRelativePath(filepath);
			}

			List<String> newFiles = new ArrayList<String>();
			for (String file : existsImgs) {
				String relExistPath = CityUtil.getMMSRelativePath(file);
				if(!isMMS){
					relExistPath = CityUtil.getUpfilesRelativePath(file);
				}				
				if (!relExistPath.equalsIgnoreCase(relFilePath)) {
					newFiles.add(file);
				}
			}

			for (String path : newFiles) {
				if(path!=null && path.length()>0)
					sb.append(path + FILE_SPERATOR);
			}
		}
		String result=sb.toString();
		return result;
	}

	public void removeMMSImage(String filepath) {
		if (getMsgattachfiles() != null) {
			String afterRemoveStr = removeImage(getMsgattachfiles().split(FILE_SPERATOR),
					filepath,true);
			this.setMsgattachfiles(afterRemoveStr);
		}
	}
	
	public void setMMSImages(List<String> images) {
		StringBuffer sb = new StringBuffer("");		
		if(images!=null && images.size()>0){
			for(int i=0;i<images.size();i++){
				String relFilePath = CityUtil.getMMSRelativePath(images.get(i));
				sb.append(relFilePath);
				if(i!=images.size()-1){
					sb.append(",");
				}
			}
		}
		setMsgattachfiles(sb.toString());
	}
	
	public void removeUploadFiles(String filepath) {
		if (getSubmitfiles() != null) {
			String afterRemoveStr = removeImage(getSubmitfiles().split(FILE_SPERATOR),
					filepath,false);
			this.setSubmitfiles(afterRemoveStr);
		}
	}
	
	public void addUploadFiles(String filepath) {
		if(filepath==null || filepath.length()<1)
			return;
		
		String existingStr = getSubmitfiles();
		// remove first to avoid duplication.
		if (getSubmitfiles() != null) {
			existingStr = removeImage(getSubmitfiles().split(FILE_SPERATOR),
					filepath,false);
		}		
		
		if(existingStr==null){
			existingStr="";
		}
		
		if(!existingStr.trim().endsWith(FILE_SPERATOR)){
			existingStr=existingStr.trim()+FILE_SPERATOR;
		}
		String result = existingStr+filepath;
		this.setSubmitfiles(result);
	}	
}